<?php
class Router
{
	protected $controllerName; //Name of controller
	protected $controllerPath; //Path to controller file
	protected $controllerNameIndex; //Name of default controller
	protected $actionName; //Name of controller's method
	protected $actionNameIndex; //Name of default index
	protected $controler; //Object of call controller

	public function __construct()
	{
		//Set default settings
		$this->controllerNameIndex = INDEX_CONTROLLER;
		$this->actionNameIndex = INDEX_ACTION;
		
		//Check authorization
		if(isset($_SESSION['AUTH']) && $_SESSION['AUTH'] == true)
		{
			//Call controlller
			$this->call();
		}
		else
		{
			//Authorization controller
			$this->controller = new ControllerAuthorization();
			$this->controller->index();
		}
	}
	/*
	*Method clear request and call controllers
	*
	*
	*return Exception
	*/
	private function call()
	{
		if(!empty($_GET['pagename']))
		{
			$this->controllerName = 'Controller'.$this->clearData($_GET['pagename']); 
			if(file_exists(CONTROLLERS_DIR.$this->controllerName.'.php'))
			{
				$this->controller = new $this->controllerName();
				if(!empty($_GET['action']))
				{
					$this->actionName = $this->clearData($_GET['action']);
					if(method_exists($this->controller,$this->actionName))
					{
						$action = $this->actionName;
						$this->controller->$action();
					}
					else
					{
						if(method_exists($this->controller,$this->actionNameIndex))
						{
							$action = $this->actionNameIndex;
							$this->controller->$action();
						}
						else
						{
							throw new Exception('Index method not exists in \''.$this->controllerName.'\' class.');
						}
					}	
				}
				else
				{
					if(method_exists($this->controller,$this->actionNameIndex))
					{
						$action = $this->actionNameIndex;
						$this->controller->$action();
					}
					else
					{
						throw new Exception('Index method not exists in \''.$this->controllerName.'\' class.');
					}
				}	
			}
			else
			{
				$this->callIndex();	
			}	
		}
		else
		{
			$this->callIndex();
		}
	}
	/*
	*Method call index action of index controller
	*
	*
	*return Exception
	*/
	private function callIndex()
	{
		$this->controllerName = 'Controller'.$this->controllerNameIndex;
		if(file_exists(CONTROLLERS_DIR.$this->controllerName.'.php'))
		{
			$this->controller = new $this->controllerName();
			if(method_exists($this->controller,$this->actionNameIndex))
			{
				$action = $this->actionNameIndex;
				$this->controller->$action();
			}
			else
			{
				throw new Exception('Router index settings error!');
			}
		}
	}
	/*
	*Method clear data 
	*
	*
	*return data
	*/
	private function clearData($data, $type='string')
	{
		switch($type)
		{
			case 'string':
				$data = (string)$data; 
				$data = strip_tags(stripslashes($data));
				$data = trim($data);
				return $data;
				break;
				
			case 'integer':
				$data = (int)$data;
				$data = abs($data);
				return $data;
				break;
				
			case 'float':
				$data = (float)$data;
				$data = abs($data);
				return $data;
				break;
		}
	}		
}
